package com.example.checkqufuface.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.checkqufuface.R;
import com.example.checkqufuface.bean.QufuPeopleCardSaleListResultBean;

import java.util.List;

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.ViewHolder> {

    private Context context;
    private List<QufuPeopleCardSaleListResultBean.DataBean.DataListBean> sexList;


    public SearchResultAdapter(Context context, List<QufuPeopleCardSaleListResultBean.DataBean.DataListBean> sexList) {
        this.context = context;
        this.sexList = sexList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_search_reuslt, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        holder.tvName.setText(sexList.get(position).getHolderName());
        if (sexList.get(position).getGender() == 0) {
            holder.tvSex.setText("男");
        }else {
            holder.tvSex.setText("女");
        }
        holder.tvIdNumber.setText(HideIdCard(sexList.get(position).getCertificateNo()));
        holder.tvPhoneNumber.setText(HideMobile(sexList.get(position).getPhoneNumber()));
        holder.tvKaTime.setText(sexList.get(position).getSaleCardTime());

    }

    @Override
    public int getItemCount() {
        return sexList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {


        private final TextView tvName;
        private final TextView tvSex;
        private final TextView tvIdNumber;
        private final TextView tvPhoneNumber;
        private final TextView tvKaTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvName = itemView.findViewById(R.id.tv_serach_adapter_name);
            tvSex = itemView.findViewById(R.id.tv_serach_adapter_sex);
            tvIdNumber = itemView.findViewById(R.id.tv_serach_adapter_id_number);
            tvPhoneNumber = itemView.findViewById(R.id.tv_serach_adapter_phone_number);
            tvKaTime = itemView.findViewById(R.id.tv_serach_adapter_ka_time);

        }
    }

    /**
     * 隐藏手机号中间四位
     *
     * @param mobile
     * @return
     */
    public static String HideMobile(String mobile) {
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(mobile) && mobile.length() > 6) {

            for (int i = 0; i < mobile.length(); i++) {
                char c = mobile.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }

        }
        return sb.toString();
    }

    /**
     * 隐藏手机号中间四位
     *
     * @param mobile
     * @return
     */
    public static String HideIdCard(String mobile) {
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(mobile) && mobile.length() > 6) {

            for (int i = 0; i < mobile.length(); i++) {
                char c = mobile.charAt(i);
                if (i >= 3 && i <= 13) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }

        }
        return sb.toString();
    }

}
